/*
* 公司：Leita Tech
* 版权信息：雷塔所有
* 任务：
* 描述：
* 作者：李大平
* 日期：2020/06/04
*
*/

#include "ui/LTJsObject.h"
#include <QDebug>
#include "model/LTModelDefines.h"

#ifdef LT_WASM
#include <emscripten/html5.h> //see: https://emscripten.org/docs/api_reference/html5.h.html
#endif

LTJsObject::LTJsObject()
{

}

#ifdef LT_WASM
EM_BOOL em_ui_resize_callback_func(int /*eventType*/, const EmscriptenUiEvent *uiEvent, void *userData)
{
    LTJsObject *js_object = static_cast<LTJsObject*>(userData);
    emit js_object->canvasResized(uiEvent->documentBodyClientWidth, uiEvent->documentBodyClientHeight);
    qDebug() << "emscripten_set_resize_callback signal emitted.";
    return true;
}
#endif

bool LTJsObject::init()
{
#ifdef LT_WASM
    //typedef EM_BOOL (*em_ui_callback_func)(int eventType, const EmscriptenUiEvent *uiEvent, void *userData);
    //EMSCRIPTEN_RESULT emscripten_set_resize_callback(const char *target, void *userData, EM_BOOL useCapture, em_ui_callback_func callback)
    if(emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, false, em_ui_resize_callback_func) < 0)
    {
        qDebug() << "emscripten_set_resize_callback failed.";
        return false;
    }
    qDebug() << "emscripten_set_resize_callback succeeded.";
#endif
    return true;
}

LTJsObject *LTJsObject::getInstance()
{
    static LTJsObject instance;
    return &instance;
}
